//
//  SXZHDetailViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/31.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXZHDetailViewController.h"
#import "SXRequestManager.h"
#import "UIView+UIView.h"
#import "UMSocial.h"
@interface SXZHDetailViewController ()<UIWebViewDelegate>
@property(nonatomic, strong) UIWebView *webView;
@property(nonatomic, strong) NSString *titleStr;
@property(nonatomic, copy) NSString *shareStr;
@end

@implementation SXZHDetailViewController
- (UIWebView *)webView
{
    if (!_webView) {
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.delegate = self;
    }
    return _webView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    self.navigationController.navigationBar.hidden = NO;
    [self requestData];
    [self setNavigationBar];
    // Do any additional setup after loading the view.
}
- (void)requestData
{
    NSString *URLStr = [kZHNewsDetailURLString stringByAppendingString:[NSString stringWithFormat:@"%ld", _IdStr]];
    [SXRequestManager requestWithType:GET urlString:URLStr parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSString *htmlStr = dic[@"body"];
        _titleStr = dic[@"title"];
        _shareStr = dic[@"share_url"];
        htmlStr = [self.webView webViewHtmlString:htmlStr imgArray:nil];
        [self.webView loadHTMLString:htmlStr baseURL:nil];
    } error:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *requestURL = request.URL;
    if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"])
        && (navigationType == UIWebViewNavigationTypeLinkClicked)) {
        return NO;
    }
    return YES;
}
- (void)setNavigationBar
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SX-yuedufanhui"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(didClickLeft)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SX-yuedushare"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(didClickRight)];
}
- (void)didClickLeft
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didClickRight
{
    [UMSocialConfig setFinishToastIsHidden:NO position:(UMSocialiToastPositionCenter)];
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"5632c41ce0f55a17390010c1"
                                      shareText:[NSString stringWithFormat:@"//分享新闻 %@ %@", _titleStr, _shareStr]
                                     shareImage:[UIImage imageNamed:@"icon.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToDouban,UMShareToRenren,UMShareToInstagram, UMShareToSms,nil]
                                       delegate:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
